#include<stdio.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
 
int main(int argc,char*argv[])
{
    if(argc!=2)
    {
        return 2;
    }

    //int fd = open("log.txt",O_WRONLY | O_TRUNC | O_CREAT);
    int fd = open("log.txt",O_WRONLY | O_APPEND | O_CREAT);
    if(fd<0)
    {
        perror("open:");
        return 1;
    }
    dup2(fd,1);
    
    fprintf(stdout,"%s\n",argv[1]);

    close(fd);
    return 0;


    //
    //int fd = open("log.txt",O_WRONLY | O_APPEND | O_CREAT);
    //if(fd<0)
    //{
    //    perror("open:");
    //    return 1;
    //}
    //fprintf(stdout,"you can see me\n");
    //fflush(stdout);
    //close(fd);
    //return 0;

    //close(0);
    //int fd = open("log.txt",O_RDONLY);
    //if(fd<0)
    //{
    //    perror("open:");
    //    return 1;
    //}
    //printf("fd:%d\n",fd);

    //char buffer[64];

    //fgets(buffer,sizeof buffer,stdin);
    //printf("%s\n",buffer);
    //close(fd);
    //return 0;





    //close(1);
    //int fd = open("log.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
    //if (fd < 0) {
    //    perror("open");
    //    return 1;
    //} 
 
    //printf("fd: %d\n", fd);
 
    //fflush(stdout);
    //close(fd);
    //return 0;
}
